<?php

namespace app\admin\service;

use app\admin\lib\exception\BaseException;
use app\admin\model\Common as CommonModel;
use app\admin\model\User as UserModel;

class UserService extends BaseService
{

    /**
     * 获取用户列表
     */
    public function getUserList(int $page, int $limit)
    {
        $result = UserModel::getList($page, $limit);
        
        return [
            'list' => $result->items(),
            'total' => $result->total(),
            'current_page_count' => $result->count(),
        ];
    }

    /**
     * 用户注册
     */
    public function register(array $data)
    {
        // 设置用户角色和状态
        $data['role'] = 0;
        $data['status'] = 1;
        
        // 调用模型添加数据
        $user = CommonModel::create($data);

        $userData = $user->hidden(['password', 'password_confirm', 'update_time', 'role', 'status'])->toArray();

        // 生成JWT token
        $tokenData = $this->getAuthService()->generateRegisterToken($user->toArray());

        return [
            'user_data' => $userData,
            'token' => $tokenData['token'],
            'expire' => $tokenData['expire']
        ];
    }
}
